
#include "area.h"
#include "avatar.h"
#include "commands-oedit.h"
#include "commandTable.h"
#include "commandTable-default.h"
#include "display.h"
#include "handler.h"
#include "room.h"
#include "symphony.h"
#include "world.h"

/*
OCmd::OCmd( void ) {
  name( "" );
  level( BUILDER );
  return;
}

bool OCmd::execute( Creature* creature, const std::string& args ) {

  return true;
}
*/

OCmdComposition::OCmdComposition( void ) {
  name( "composition" );
  level( BUILDER );
  syntax( "syntax: composition add <type1> [type2 type3...]\n        composition remove <type1> [type2 type3...]\n\nValid compositions: wood paper iron leather" );
  return;
}

bool OCmdComposition::execute( Creature* creature, const std::string& args ) {
  std::vector<std::string> pieces = Regex::split( args, 5 );
  bool value = false;

  if ( pieces.size() < 2 ) {
    creature->send( syntax() );
    return false;
  }

  if ( Regex::strPrefix( pieces[0], "add" ) ) {
    value = true;
  } else if ( Regex::strPrefix( pieces[0], "remove" ) ) {
    value = false;
  } else {
    creature->send( syntax() );
    return false;
  }

  pieces.erase( pieces.begin() );

  for ( std::vector<std::string>::iterator it = pieces.begin(); it != pieces.end(); ++it ) {
    creature->oedit()->comp( Object::getCompositionType( *it ), value );
  }

  creature->send( "Object composition is now: %s", creature->oedit()->listComposition().c_str() );
  return true;
}

OCmdDelete::OCmdDelete( void ) {
  name( "delete" );
  level( BUILDER );
  syntax( "syntax: delete delete" );
  return;
}

bool OCmdDelete::execute( Creature* creature, const std::string& args ) {
  CmdExit exit;
  Area* area = NULL;
  Object* object = creature->oedit();
  unsigned long vnum = object->vnum();

  if ( args != "delete" ) {
    creature->send( syntax() );
    return false;
  }

  if ( ( area = World::Instance().lookup( vnum ) ) == NULL ) {
    creature->send( "Error establishing vnum-area association." );
    return false;
  }

  area->objects().erase( vnum );
  object->destroy();
  creature->oedit( NULL );
  creature->send( "Object %lu deleted.\n", vnum );
  exit.execute( creature, "" );

  return true;
}

OCmdDescription::OCmdDescription( void ) {
  name( "description" );
  level( BUILDER );
  syntax( "syntax: description <text>" );
  return;
}

bool OCmdDescription::execute( Creature* creature, const std::string& args ) {

  if ( args.empty() ) {
    creature->send( syntax() );
    return false;
  }

  creature->oedit()->description( args );
  creature->send( "You've set the object description to \"%s\".", creature->oedit()->description().c_str() );

  return true;
}

OCmdExtraflag::OCmdExtraflag( void ) {
  name( "extraflag" );
  level( BUILDER );
  syntax( "syntax: extraflag add <type1> [<type2> <type3>...]\n        extraflag remove <type1> [<type2> <type3>...]\n\nValid extra flags are...\n For containers: closeable closed lockable locked burnproof\n For weapons: sharp shocking burning freezing" );
  return;
}

bool OCmdExtraflag::execute( Creature* creature, const std::string& args ) {
  std::vector<std::string> pieces = Regex::split( args, 1 );
  std::vector<std::string> flags;
  bool value = false;

  if ( pieces.size() < 2 ) {
    creature->send( syntax() );
    return false;
  }

  flags = Regex::explode( " ", pieces[1], true );

  if ( Regex::strPrefix( pieces[0], "add" ) ) {
    value = true;
  } else if ( Regex::strPrefix( pieces[0], "remove" ) ) {
    value = false;
  } else {
    creature->send( syntax() );
    return false;
  }

  for ( std::vector<std::string>::iterator it = flags.begin(); it != flags.end(); ++it ) {
    creature->oedit()->flag( Object::getFlagType( *it, creature->oedit()->type() ), value );
  }

  creature->send( "Object extra flags are now: %s", creature->oedit()->listFlags().c_str() );
  return true;
}

OCmdFlag::OCmdFlag( void ) {
  name( "flag" );
  level( BUILDER );
  syntax( "syntax: flag add <type1> [<type2> <type3>...]\n        flag remove <type1> [<type2> <type3>...]\n\nValid flags: noremove nodrop meltdrop glowing humming" );
  return;
}

bool OCmdFlag::execute( Creature* creature, const std::string& args ) {
  std::vector<std::string> pieces = Regex::split( args, 1 );
  std::vector<std::string> flags;
  bool value = false;

  if ( pieces.size() < 2 ) {
    creature->send( syntax() );
    return false;
  }

  flags = Regex::explode( " ", pieces[1], true );

  if ( Regex::strPrefix( pieces[0], "add" ) ) {
    value = true;
  } else if ( Regex::strPrefix( pieces[0], "remove" ) ) {
    value = false;
  } else {
    creature->send( syntax() );
    return false;
  }

  for ( std::vector<std::string>::iterator it = flags.begin(); it != flags.end(); ++it ) {
    creature->oedit()->basicFlag( Object::getFlagType( *it ), value );
  }

  creature->send( "Object flags are now: %s", creature->oedit()->listBasicFlags().c_str() );
  return true;
}

OCmdInformation::OCmdInformation( void ) {
  name( "information" );
  level( BUILDER );
  return;
}

bool OCmdInformation::execute( Creature* creature, const std::string& args ) {
  creature->send( Object::getInformation( creature->oedit() ) );
  return true;
}

OCmdKeywords::OCmdKeywords( void ) {
  name( "keywords" );
  level( BUILDER );
  syntax( "syntax: keywords <text>" );
  return;
}

bool OCmdKeywords::execute( Creature* creature, const std::string& args ) {
  std::vector<std::string> keywords;

  if ( args.empty() ) {
    creature->send( syntax() );
    return false;
  }

  keywords = Regex::explode( " ", args );
  creature->oedit()->getKeywords().clear();
  for ( std::vector<std::string>::iterator it = keywords.begin(); it != keywords.end(); ++it ) {
    creature->oedit()->addKeyword( *it );
  }
  creature->send( "You've set the object keywords to \"%s\".", creature->oedit()->getKeywordList().c_str() );

  return true;
}

OCmdLevel::OCmdLevel( void ) {
  name( "level" );
  level( BUILDER );
  return;
}

bool OCmdLevel::execute( Creature* creature, const std::string& args ) {
  creature->oedit()->level( estring(args) );
  creature->send( "You've set the object level to %u.", creature->oedit()->level() );
  return true;
}

OCmdShortname::OCmdShortname( void ) {
  name( "shortname" );
  level( BUILDER );
  syntax( "syntax: shortname <text>" );
  return;
}

bool OCmdShortname::execute( Creature* creature, const std::string& args ) {

  if ( args.empty() ) {
    creature->send( syntax() );
    return false;
  }

  creature->oedit()->shortname( args );
  creature->send( "You've set the object shortname to \"%s\".", creature->oedit()->shortname().c_str() );

  return true;
}

OCmdValue::OCmdValue( void ) {
  name( "value" );
  level( BUILDER );
  return;
}

bool OCmdValue::execute( Creature* creature, const std::string& args ) {
  creature->oedit()->value( estring(args) );
  creature->send( "You've set the object value to %u.", creature->oedit()->value() );
  return true;
}

